package com.project.controller;

import com.project.bean.BuyCartBean;
import com.project.bean.BuyOrderBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCartService;
import com.project.service.IBuyDetailService;
import com.project.service.IBuyOrderService;
import com.project.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("buyOrder")
@Api(tags = "采购订单")
public class BuyOrderController {
    @Autowired
    private IBuyOrderService orderService;
    @Autowired
    private IBuyCartService buyCartService;
    @Autowired
    private IBuyDetailService detailService;

    @ApiOperation(value = "入库")
    @PostMapping ("add")
    public String add(String customName, HttpServletRequest request) {
        //从session中取出登录用户
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        //添加订单
        BuyOrderBean buyOrderBean = new BuyOrderBean(customName,user.getName());
        orderService.add(buyOrderBean);
        //根据登陆用户id，查询购物车集合
        List<BuyCartBean> list = buyCartService.findById(user.getId());
        //将采购信息的ID和购车集合，批量添加到采购明细中。同时修改库存商品的最新采购价，累加商品库存数量
        detailService.addDetail(buyOrderBean.getId(),list);
        //按登陆用户ID，删除购物车信息
        buyCartService.delByUserId(user.getId());
        return "ok";
    }

}
